define(['jquery'],function($){
    /*
     *
     *
     */
    $.fn.createmap=function(a,callback){
        var defaults={
            map: {
                key: "QdfXLGhCAhnYPvP29XSWgQMAjXyVIQ0p",
                id: null,
                //center: {x: 116.395645,y: 39.929986},//38.65777,104.08296
                center: {x: 114.0661120000,y:22.5485150000},//22.5647700000,114.1080630000
                city: "深圳市",
                level: 12
            },
            event:{dra:1,scr:1,dou:1,key:1},
            control:{
                nav:{self:1,style:"LARGE",place:"TOP_LEFT"},
                ove:{self:1,style:1,place:"BOTTOM_RIGHT"},
                sca:{self:1,style:"METRIC",place:"BOTTOM_LEFT"}
            }
        };
        return this.each(function() {
            var that = $.extend({},defaults,a);
            var api={};
            var t = $(this);
			if(t.length==0) return false;

            var mapid= t.attr('id');
            that.map.id=mapid;

            var mpoint=$(that.input).val();
            mpoint && (mpoint=mpoint.split(','),that.map.center.x=mpoint[0],that.map.center.y=mpoint[1]);

            var userConfig = {
                map: {
                    key: "",
                    id: that.map.id,
                    center: that.map.center,
                    city: that.map.city,
                    level: that.map.level
                },
                event: {
                    dra: that.event.dra,
                    scr: that.event.scr,
                    dou: that.event.dou,
                    key: that.event.key
                },
                control: {
                    nav: {
                        self: that.control.nav.self,
                        style: that.control.nav.style,
                        place: that.control.nav.place
                    },
                    ove: {
                        self: that.control.ove.self,
                        style: that.control.ove.style,
                        place: that.control.ove.place
                    },
                    sca: {
                        self: that.control.sca.self,
                        style: that.control.sca.style,
                        place: that.control.sca.place
                    }
                }
            };
            var userCcof = userConfig.control;
            var map;

            //标注点数组
            var myMarker = {title:"",content:"",point:$(that.input).val(),isOpen:0,icon:{w:23,h:25,l:46,t:21,x:9,lb:12}};
            var curMarker = null;

            initMap();
            function initMap(){
                createMap();//创建地图
                setMapEvent();//设置地图事件
                addMapControl();//向地图添加控件
                addMarker();//向地图中添加marker
            }
            //创建地图函数：
            function createMap(){
                var c = userConfig.map;

                map = new BMap.Map(mapid);//在百度地图容器中创建一个地图
                var a = new BMap.Point(c.center.x, c.center.y);
                map.centerAndZoom(a, c.level);

                map.addEventListener("click", getXy);
            }
            //地图事件设置函数：
            function setMapEvent(){
                map.enableDragging();//启用地图拖拽事件，默认启用(可不写)
                map.enableScrollWheelZoom();//启用地图滚轮放大缩小
                map.enableDoubleClickZoom();//启用鼠标双击放大，默认启用(可不写)
                map.enableKeyboard();//启用键盘上下左右键移动地图
            }

            //地图控件添加函数：
            function addMapControl(){
                map.addControl(new BMap.NavigationControl()); // 添加平移缩放控件
                map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
            }

            //创建marker
            function addMarker(){
                var json = myMarker;
                var p0 = json.point.split(",")[0];
                var p1 = json.point.split(",")[1];
                var point = new BMap.Point(p0,p1);
                var iconImg = createIcon(json.icon);
                var marker = new BMap.Marker(point,{icon:iconImg});
                map.addOverlay(marker);
                curMarker = marker;
            }
            //创建InfoWindow
            function createInfoWindow(i){
                var json = markerArr[i];
                var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>"+json.content+"</div>");
                return iw;
            }
            //创建一个Icon
            function createIcon(json){
                var icon = new BMap.Icon("http://app.baidu.com/map/images/us_mk_icon.png", new BMap.Size(json.w,json.h),{imageOffset: new BMap.Size(-json.l,-json.t),infoWindowOffset:new BMap.Size(json.lb+5,1),offset:new BMap.Size(json.x,json.h)})
                return icon;
            }

            function getXy(e){
                //alert(e.point.lng + ", " + e.point.lat);
                document.getElementById('map').value=e.point.lng + "," + e.point.lat;
                curMarker.setPoint(e.point);

                if($.isFunction(that.fn)){
                    var gc = new BMap.Geocoder();
                    //获取地址的数据地址
                    gc.getLocation(e.point, function(rs){
                        that.fn(rs.addressComponents);
                    });
                }   
            }

            function updateCurrentCity(a) {
                a.replace(/^\s*|\s*$/g, "");
                if (!a) {
                    //请输入正确的中文城市名称
                    return
                }
                getCityPoint(a);
            }

            function getCityPoint(b) {
                b = encodeURIComponent(b);
                var a = "http://map.baidu.com/?newmap=1&qt=cur&ie=utf-8&wd=" + b + "&oue=1&res=jc";

                $.ajax({
                    type: "GET",
                    url: a,
                    jsonp:'callback',
                    dataType: "jsonp",
                    success: function(data){
                        setCurrentCity(data);
                    }
                });
            }
            function setCurrentCity(b) {
                if (b.content.error == 0) {

                } else {
                    var d = (((b.content.geo).split("|")[2]).split(";")[0]).split(",")[0];
                    var c = (((b.content.geo).split("|")[2]).split(";")[0]).split(",")[1];
                    var a = map.mercatorToLnglat(d, c);
                    if (b.content.cname == "全国") {
                        map.centerAndZoom(new BMap.Point(a[0], a[1]), 5)
                    } else {
                        map.centerAndZoom(new BMap.Point(a[0], a[1]), b.content.level)
                    }
                }
            }

            function getlikeArea(a) {
                if (typeof(l_search) == "undefined") {
                    l_option = {
                        onSearchComplete: function(e) {
                            if (l_search.getStatus() == BMAP_STATUS_SUCCESS) {
                                var b = l_search._json.content.level || 17;
                                var g = 0;
                                if (l_search._json.content.length) {
                                    g = l_search._json.content.length - e.getCurrentNumPois();
                                    var h = "";
                                    var c = l_search._json.content[g].cla;
                                    for (var f = 0; f < c.length; f++) {
                                        h += c[f].splice(",")[1] + ","
                                    }
                                    if (/区县/g.test(h)) {
                                        b = 14
                                    }
                                }
                                var k = e.getPoi(0);
                                var d = k.point;
                                var j = new BMap.Point(d.lng, d.lat);
                                map.centerAndZoom(j, b)
                            }
                        }
                    };
                    l_search = new BMap.LocalSearch(map, l_option)
                }

                if (!a.replace(/^\s*|\s*$/g, "")) {
                    return
                }
                l_search.search(a)
            }

            api.init=initMap;
            api.updateCurrentCity=updateCurrentCity;
            api.getlikeArea=getlikeArea;

            //api.mapData=mapData;
            //api.TipContents=TipContents;
            if ($.isFunction(callback)) callback(api);
        });
    };
});